// 栈的顺序实现 
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE  100 //栈的初始化容量,如果存储的是int,可以存储25个元素
#define STACKINCREASE 10 //栈的增量  
// 栈的数据结构  
typedef struct stack{
    int* base;  //用于存储元素,数组
    int* top; //栈顶指针  
    int stackSize; //栈的大小,字节为单位
}Stack;
Stack stackInit();
void clearStack(Stack stack);
int isempty(Stack stack);
int StackLength(Stack stack);
int getTop(Stack stack);
void pushStack(Stack stack,int data);
int popStack(Stack stack);
int main(){
   //test
   Stack stack=stackInit();
   pushStack(stack,1);
   pushStack(stack,2);
   pushStack(stack,3);
   pushStack(stack,4);
   pushStack(stack,5);
   printf("栈顶元素%d",getTop(stack));

    return 0;
}
Stack stackInit(){
    Stack stack;
    stack.base=(int*)malloc(STACK_INIT_SIZE);
    stack.top=stack.base;
    stack.stackSize=STACK_INIT_SIZE;
    return stack;
}
void clearStack(Stack stack){
    stack.top=stack.base;
    stack.stackSize=0;
}
int isempty(Stack stack){
    if(stack.top==stack.base){
        return 1;
    }
    else{    return 0;
}
}
int StackLength(Stack stack){
    return stack.stackSize/sizeof(int);
}
int getTop(Stack stack){
    return *(stack.top-1);
}
void pushStack(Stack stack,int data){
        if((stack.top-stack.base)*sizeof(int)==stack.stackSize){// 栈满
                int* temp=(int*)malloc(stack.stackSize+STACKINCREASE);
                temp=stack.base;
                stack.base=temp;
                stack.top++;
                *(stack.top-1)=data;
                stack.stackSize+=sizeof(int);
        }
        else{
            *stack.top++=data;
            stack.stackSize+=sizeof(int);
        }
}
int popStack(Stack stack){
    if(isempty(stack)==1){
        printf("栈空,弹栈失败\n");
        return -1;
    }
    else{
        stack.top--;
        return *(stack.top);
    }
}